package com.dks.controller.student;

import com.dks.dto.BaseIdRequestDto;
import com.dks.dto.student.UserClassHourPackageBuyDto;
import com.dks.dto.student.UserClassHourPackageGiveDto;
import com.dks.dto.student.UserClassHourPackageTransferDto;
import com.dks.enumeration.UserClassHourPackageStatus;
import com.dks.exception.BaseException;
import com.dks.models.student.ClassHourPackage;
import com.dks.models.student.UserClassHourPackage;
import com.dks.repository.IUserClassHourPackageRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 课时包相关
 */
@RestController
@RequestMapping("user-class-hour-package")
public class UserClassHourPackageController {

    @Autowired
    private IUserClassHourPackageRepository userClassHourPackageRepository;

    /**
     * 课时包-购买
     *
     * @author kenny
     */
    @PostMapping("buy")
    public ResultModel<Object> buy(@RequestBody @Validated UserClassHourPackageBuyDto userClassHourPackageBuyDto) throws BaseException, WxPayException {
        return userClassHourPackageRepository.buy(userClassHourPackageBuyDto);
    }

    /**
     * 课时包-赠送
     *
     * @author evan
     */
    @PostMapping("give")
    public ResultModel<Object> give(@RequestBody @Validated UserClassHourPackageGiveDto userClassHourPackageGiveDto) throws BaseException, WxPayException {
        return userClassHourPackageRepository.give(userClassHourPackageGiveDto);
    }

    /**
     * 课时包-开卡
     *
     * @author evan
     */
    @PostMapping("open")
    public ResultModel<Boolean> open(@RequestBody @Validated BaseIdRequestDto baseIdRequestDto) {
        return userClassHourPackageRepository.open(baseIdRequestDto);
    }

    /**
     * 课时包-转移申请
     *
     * @author evan
     */
    @PostMapping("transfer-apply")
    public ResultModel<Boolean> transferApply(@RequestBody @Validated UserClassHourPackageTransferDto transferDto) {
        return userClassHourPackageRepository.transferApply(transferDto);
    }

    /**
     * 课时包-取消转移申请
     *
     * @author evan
     */
    @PostMapping("transfer-cancel")
    public ResultModel<Boolean> transferApplyCancel(@RequestBody @Validated BaseIdRequestDto baseIdRequestDto) {
        return userClassHourPackageRepository.transferApplyCancel(baseIdRequestDto);
    }

    /**
     * 课时包-退费申请
     *
     * @author evan
     */
    @PostMapping("refund-apply")
    public ResultModel<Boolean> refundApply(@RequestBody @Validated BaseIdRequestDto baseIdRequestDto) {
        return userClassHourPackageRepository.refundApply(baseIdRequestDto);
    }


    /**
     * 查询用户所有的课时包
     *
     * @author evan
     */
    @GetMapping("list")
    public ResultModel<List<UserClassHourPackage>> list(@RequestParam("userId") Long userId,
                                                        @RequestParam(value = "status", required = false) UserClassHourPackageStatus status) {
        if (userId == null) {
            return Result.makeResultAppend(ResultInfo.ERR_BASE_001, "userId 用户id");
        }
        if (status == null) {
            return userClassHourPackageRepository.list(userId, null);
        } else {
            return userClassHourPackageRepository.list(userId, status.name());
        }
    }

    /**
     * 查询用户单个课包详情
     *
     * @author evan
     */
    @GetMapping("detail/{id}")
    public ResultModel<UserClassHourPackage> detail(@PathVariable(name = "id") Long packageId, @RequestParam("userId") Long userId) {
        if (userId == null) {
            return Result.makeResultAppend(ResultInfo.ERR_BASE_001, "userId 用户id");
        }
        if (packageId == null) {
            return Result.makeResultAppend(ResultInfo.ERR_BASE_001, "packageId 课时包ID");
        }
        return userClassHourPackageRepository.getUserClassHourPackageDetail(packageId, userId);
    }

    /**
     * 查询系统所有的课时包
     *
     * @author evan
     */
    @GetMapping("system-list")
    public ResultModel<List<ClassHourPackage>> systemList(Integer pageNum, Integer pageSize) {
        if (null == pageNum) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "pageNum");
        }
        if (null == pageSize) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "pageSize");
        }
        return userClassHourPackageRepository.systemList(pageNum, pageSize);

    }

    /**
     * 查询系统课时包详情
     *
     * @author kenny
     */
    @GetMapping("system-detail/{id}")
    public ResultModel<ClassHourPackage> systemDetail(@PathVariable(name = "id") Long id) {
        return userClassHourPackageRepository.getSysPackageDetail(id);

    }

}
